<?php
    class TokenBLL{
        //生成token
        public function generatingToken($uid,$level){
            //时间戳
            $time=intval(time());
            //原文
            $originalText='uid='.$uid.'-level='.$level.'-time='.$time;
            //密文
            $token=$ciphertext=md5(sha1($originalText));

            return $token;
        }

        //验证token
        public function verificationToken($token){
            $result=false;
            if($this->isToken($token)){
                $tokenInfo=$this->getTokenInfo($token);
                $tokenTime=$tokenInfo['modification_time'];
                $nowTime=date('y-m-d h:i:s');
                $day=floor((strtotime($nowTime)-strtotime($tokenTime))/86400);
                if($day<7){
                    $result=$tokenInfo;
                }
                //$hour=floor((strtotime($nowTime)-strtotime($tokenTime))%86400/3600);
                //$minute=floor((strtotime($nowTime)-strtotime($tokenTime))%86400/60);
                //$second=floor((strtotime($nowTime)-strtotime($tokenTime))%86400%60);
            }
            
            return $result;
        }

        //获取token信息
        public function getTokenInfo($token){
            $result=false;
            if(!$token==null){
                $tokenDAL=new TokenDAL();
                $result=$tokenDAL->getTokenInfo($token);
            }
            return $result;
        }

        //保存token
        public function saveToken($uid,$token){
            $flag=false;
            $tokenDAL=new TokenDAL();
            if($this->isUserToken($uid)){
                $flag=$tokenDAL->updateToken($uid,$token);
            }else{
                $flag=$tokenDAL->insertToken($uid,$token);
            }

            return $flag;
        }

        //判断用户是否已存在token
        public function isUserToken($uid){
            $flag=false;
            $tokenDAL=new TokenDAL();
            $result=$tokenDAL->isUserToken($uid);
            if($result['count']>0){
                $flag=true;
            }
            return $flag;
        }
        //判断token是否已存在
        public function isToken($token){
            $flag=false;
            $tokenDAL=new TokenDAL();
            $result=$tokenDAL->isToken($token);
            if($result['count']>0){
                $flag=true;
            }
            return $flag;
        }
    }
    
?>